﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlarmManagePlatform.UC
{
    public partial class SelectImage : UserControl
    {
        public SelectImage()
        {
            InitializeComponent();
            pImage.DoubleClick += PImage_DoubleClick;
            InitControls();
        }

        private void PImage_DoubleClick(object sender, EventArgs e)
        {
            if (PanelImage == null) return;
            Forms.ImagePreview imagePreview = new Forms.ImagePreview();
            imagePreview.ImagePicture = PanelImage;
            imagePreview.Show();
        }

        public string ImageString
        {
            get
            {
                if (pImage.BackgroundImage != null)
                {
                    return  Relations.Common.Methods.ImageToBase64String((Bitmap)pImage.BackgroundImage);
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public Bitmap PanelImage
        {
            get
            {
                if (pImage.BackgroundImage != null)
                {

                    Bitmap _bitmap = new Bitmap(pImage.BackgroundImage);
                    return _bitmap;
                }
                else
                {
                    return null;
                }
            }
            set
            {
                pImage.BackgroundImage = null;
                pImage.BackgroundImage = value;
            }
        }

        private void InitControls()
        {
            pImage.BackgroundImageLayout = ImageLayout.Zoom;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            pImage.BackgroundImage = null;
            btnSelect.Text = "选择";
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            btnSelect.Text = "重择";

            System.Windows.Forms.OpenFileDialog _OpenFile = new OpenFileDialog();
            _OpenFile.Multiselect = false;
            _OpenFile.Filter = "Image File (*.jpg,*.jpeg,*.png)|*.jpg;*.jpeg;*.png";

            _OpenFile.RestoreDirectory = true;

            if (_OpenFile.ShowDialog() == DialogResult.OK)
            {
                Bitmap bitmap = new Bitmap(_OpenFile.FileName);
                pImage.BackgroundImage = bitmap;
                
            }
        }
    }
}
